<?php
/**
 * Copyright (c) Enalean, 2016 - 2017. All Rights Reserved.
 * Copyright 1999-2000 (c) The SourceForge Crew
 *
 * This file is a part of Tuleap.
 *
 * Tuleap is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Tuleap is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tuleap. If not, see <http://www.gnu.org/licenses/>.
 */

use Tuleap\FRS\FRSPermissionDao;
use Tuleap\FRS\FRSPermissionFactory;
use Tuleap\FRS\FRSPermissionManager;
use Tuleap\FRS\ReleasePermissionManager;

require_once('pre.php');
require_once('www/file/file_utils.php');
require_once('common/frs/FRSReleaseFactory.class.php');
require_once('common/reference/CrossReferenceFactory.class.php');

if($request->valid(new Valid_UInt('release_id'))) {
    $release_id = $request->get('release_id');
} else {
    exit_error($GLOBALS['Language']->getText('file_shownotes','not_found_err'),$GLOBALS['Language']->getText('file_shownotes','release_not_found'));
}

$frsrf   = new FRSReleaseFactory();
$user    = UserManager::instance()->getCurrentUser();
$release = $frsrf->getFRSReleaseFromDb($release_id);

$permission_manager = new FRSPermissionManager(
    new FRSPermissionDao(),
    new FRSPermissionFactory(new FRSPermissionDao())
);
$release_permission_manager = new ReleasePermissionManager($permission_manager , $frsrf);
if ($release_permission_manager->canUserSeeRelease($user, $release, $release->getProject()) === false) {
    exit_error($Language->getText('file_shownotes','not_found_err'),$Language->getText('file_shownotes','release_not_found'));
}

$additional_view = getAdditionalView($release, $user);

$group_id = $release->getGroupID();
file_utils_header(array('title'=>$Language->getText('file_shownotes','release_notes'),'group'=>$group_id));

if ($additional_view) {
    echo $additional_view;
} else {
    $hp = Codendi_HTMLPurifier::instance();

    $HTML->box1_top($Language->getText('file_shownotes','notes'));

    echo '<h3>'.$Language->getText('file_shownotes','release_name').': <A HREF="showfiles.php?group_id='.$group_id.'">'.$hp->purify($release->getName()).'</A></H3>
        <P>';

/*
    Show preformatted or plain notes/changes
*/
    $purify_level = CODENDI_PURIFIER_BASIC;
    if ($release->isPreformatted()) {
        echo '<PRE>'.PHP_EOL;
        $purify_level = CODENDI_PURIFIER_BASIC_NOBR;
    }
    echo '<B>'.$Language->getText('file_shownotes','notes').':</B>'.PHP_EOL
         .$hp->purify($release->getNotes(), $purify_level, $group_id).
        '<HR NOSHADE SIZE=1>'.
        '<B>'.$Language->getText('file_shownotes','changes').':</B>'.PHP_EOL
        .$hp->purify($release->getChanges(), $purify_level, $group_id);
    if ($release->isPreformatted()) {
        echo '</PRE>';
    }

    $crossref_fact= new CrossReferenceFactory($release_id, ReferenceManager::REFERENCE_NATURE_RELEASE, $group_id);
    $crossref_fact->fetchDatas();
    if ($crossref_fact->getNbReferences() > 0) {
        echo '<hr noshade>';
        echo '<b> '.$Language->getText('cross_ref_fact_include','references').'</b>';
        $crossref_fact->DisplayCrossRefs();
    }
}

$HTML->box1_bottom();

file_utils_footer(array());


function getAdditionalView(FRSRelease $release, PFUser $user)
{
    $view = '';
    $params = array(
        'release' => $release,
        'user'    => $user,
        'view'    => &$view
    );

    EventManager::instance()->processEvent('frs_release_view', $params);

    return $view;
}
